﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StateAgent
{
    public class StateMachine<T>
    {
        private T owner;

        private BaseState<T> currentState = null;
        private BaseState<T> previousState = null;
        private BaseState<T> globalState = null;

        public StateMachine(T owner)
        {
            this.owner = owner;
        }

        public BaseState<T> CurrentState
        {
            get { return currentState; }
            set { currentState = value; }
        }

        public BaseState<T> PreviousState
        {
            get { return previousState; }
            set { previousState = value; }
        }

        public BaseState<T> GlobalState
        {
            get { return globalState; }
            set { globalState = value; }
        }

        public void Update()
        {
            if (globalState != null) globalState.Execute(owner);
            if (currentState != null) currentState.Execute(owner);
        }

        public void ChangeState(BaseState<T> newState)
        {
            previousState = currentState;
            
            currentState.Exit(owner);
            currentState = newState;
            currentState.Enter(owner);
        }

        public void RevertState()
        {
            ChangeState(previousState);
        }
    }
}
